【Perl】与【Excel】

news2024/10/6 14:24:54



引言

perl脚本语言对于文本的处理、转换很强大。对于一些信息量庞大的文本文件,看起来不直观,可以将信息提取至excel表格中,增加数据分析的可视化。perl语言的cpan提供了大量模块。对于excel文件的操作主要用到模块:

Spreadsheet::ParseXLSX

Excel::Writer::XLSX

第一个用于对现有excel 表格的解析,第二个用于创建新的excel文件。如果单纯是将文本信息提取到excel表格中其实第二个模块用的更多。看自己需求吧。

参考 & 鸣谢:

perl处理Excel(跨平台) - 潘高的小站 (pangao.vip)



模块安装

首先确保你的linux系统有perl。

安装模块的指令 sudo perl -MCPAN -e "install '模块名'"

sudo perl -MCPAN -e "install 'Spreadsheet::ParseXLSX'"

sudo perl -MCPAN -e "install 'Excel::Writer::XLSX'"

安装成功的标志:

示例

纯粹为了练习而写的脚本,统计学生信息,名字是随机生成的字符串。

#!/usr/bin/perl

=pod
==========================================
Purpose : Excel Witer Example
Author  : On the way , running
Date      : 2024-06-16
==========================================
=cut

use warnings;
use Excel::Writer::XLSX;

# -------------------Create a new Excel workbook
my $xlsx_file_name = "Example.xlsx";
my $workbook = Excel::Writer::XLSX->new( $xlsx_file_name );
print "Create excel file finished!\n";
 
# Add worksheet
$worksheet1 = $workbook->add_worksheet("Class_1_Information");
$worksheet2 = $workbook->add_worksheet("Class_2_Information");
$worksheet3 = $workbook->add_worksheet("Class_3_Information");


# -------------------Add and define format
$format_first_row = $workbook->add_format();
$format_first_row->set_bold();
# $format_first_row->set_italic();
$format_first_row->set_color( 'white' );
$format_first_row->set_align( 'center' );
$format_first_row->set_align( 'vcenter' );
$format_first_row->set_bg_color( 'blue' );
$format_first_row->set_border(1);

$format_other_row = $workbook->add_format();
# $format_other_row->set_bold();
# $format_other_row->set_italic();
$format_other_row->set_color( 'black' );
$format_other_row->set_align( 'center' );
$format_other_row->set_align( 'vcenter' );
$format_other_row->set_bg_color( 'white' );
$format_other_row->set_border(1);

# -------------------Define functions 
# aim to : generate rand data  
# call format : func_gen_rand_int_data($lower_bound , $upper_bound);
sub func_gen_rand_int_data {
	return int(rand($_[1] - $_[0] + 1)) + $_[0];
}
# function test 
print "The generated rand data is " . func_gen_rand_int_data(0,1) . "\n";

# aim to : generate rand string  
# call format : func_gen_rand_string($lower_bound , $upper_bound , $string_length);
sub func_gen_rand_string {
	my $string_out;
	for (my $i = 0; $i < $_[2]; $i++) {
		$string_out .= chr(func_gen_rand_int_data( $_[0] , $_[1] ));
	}
	return $string_out;
}
# function test 
print "The generated rand string is " . func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) . "\n";
print "The generated rand string is " . func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) . "\n";


# -------------------Set information
my $class1_student_num = func_gen_rand_int_data(40 , 50);
my $class2_student_num = func_gen_rand_int_data(40 , 50);
my $class3_student_num = func_gen_rand_int_data(40 , 50);

my @table_head = ("Index" , "Student_Name" , "Gender" , "Age" , "Interest");
my @table_Gender = ("Male" , "Female");
my @table_Interest = ("Ping-Pong" , "Football" , "Basketball" , "Swimming" , "Hiking" , "Climbing" , "Game");

# set cell width/height
$worksheet1->set_column('A:E',30);$worksheet1->set_row(0,30);
$worksheet2->set_column('A:E',30);$worksheet2->set_row(0,30);
$worksheet3->set_column('A:E',30);$worksheet3->set_row(0,30);

#-------------------Write Information to excel file

for (my $col = 0; $col < 5; $col++) {
	for (my $row = 0; $row < $class1_student_num; $row++) {
		if($row == 0){
			$worksheet1->write( $row, $col, $table_head[$col], $format_first_row );
			}
		else{
			if ($col == 0) {
				$worksheet1->write( $row, $col, $row , $format_other_row );
			}
			elsif($col == 1) {
				$worksheet1->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );
			}
			elsif($col == 2) {
				$worksheet1->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );
			}
			elsif($col == 3) {
				$worksheet1->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );
			}
			else{
				$worksheet1->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );
			}
		}
	}
}


for (my $col = 0; $col < 5; $col++) {
	for (my $row = 0; $row < $class2_student_num; $row++) {
		if($row == 0){
			$worksheet2->write( $row, $col, $table_head[$col], $format_first_row );
			}
		else{
			if ($col == 0) {
				$worksheet2->write( $row, $col, $row , $format_other_row );
			}
			elsif($col == 1) {
				$worksheet2->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );
			}
			elsif($col == 2) {
				$worksheet2->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );
			}
			elsif($col == 3) {
				$worksheet2->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );
			}
			else{
				$worksheet2->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );
			}
		}
	}
}

for (my $col = 0; $col < 5; $col++) {
	for (my $row = 0; $row < $class3_student_num; $row++) {
		if($row == 0){
			$worksheet3->write( $row, $col, $table_head[$col], $format_first_row );
			}
		else{
			if ($col == 0) {
				$worksheet3->write( $row, $col, $row , $format_other_row );
			}
			elsif($col == 1) {
				$worksheet3->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );
			}
			elsif($col == 2) {
				$worksheet3->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );
			}
			elsif($col == 3) {
				$worksheet3->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );
			}
			else{
				$worksheet3->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );
			}
		}
	}
}
print "Write Done ! please input < soffice $xlsx_file_name > command in you terminal to open the excel file !\n";

 
$workbook->close();

结果示意:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1830085.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

1970python高校教室管理系统mysql数据库Django框架bootstrap布局计算机软件工程网页

一、源码特点 python Django 高校教室管理系统是一套完善的web设计系统mysql数据库 &#xff0c;对理解python编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。 python Django 高校教室管理系统 开发环境pycharm mys…

2.华为配置静态路由

通过配置静态路由让PC1和PC2互通 AR1 [Huawei]int g0/0/0 [Huawei-GigabitEthernet0/0/0]ip add 192.168.1.254 24 [Huawei]int g0/0/1 [Huawei-GigabitEthernet0/0/1]ip add 1.1.1.1 24 [Huawei]ip route-static 192.168.2.0 24 1.1.1.2AR2 [Huawei]int g0/0/0 [Huawei-Gig…

ARM Linux 设备树详细介绍(2)共二篇

承接上文&#xff0c;第一篇 3. Device&Tree 引发的 BSP 和驱动变更 有了 Device Tree 后&#xff0c;大量的板级信息都不再需要&#xff0c;譬如过去经常在 arch/arm/plat-xxx 和 arch/arm/mach-xxx 实施的如下事情&#xff1a; 1. 注册 platform_device&#xff0c;绑定 …

《人生海海》读后感

麦家是写谍战的高手&#xff0c;《暗算》《风声》等等作品被搬上荧屏后&#xff0c;掀起了一阵一阵的收视狂潮。麦家声名远扬我自然是知道的&#xff0c;然而我对谍战似乎总是提不起兴趣&#xff0c;因此从来没有拜读过他的作品。这几天无聊时在网上找找看看&#xff0c;发现了…

从xxl-job源码中学习Netty的使用

1. 启动与Spring实例化 com.xxl.job.core.executor.impl.XxlJobSpringExecutor.java类 继承SmartInitializingSingleton 类&#xff0c;在afterSingletonsInstantiated 实例化后方法中 调用initJobHandlerMethodRepository 把所有的xxljob任务管理起来&#xff1b; private…

ARM Linux 设备树详细介绍(1)

1. ARM&Device&Tree 起源 Linus Torvalds 在 2011 年 3 月 17 日的 ARM Linux 邮件列表宣称“this whole ARM thing is a f*cking pain in the ass”&#xff0c;引发 ARM Linux 社区的地震&#xff0c;随后 ARM 社区进行了一系列 的重大修正。 在过去的 ARM Linux 中&…

电子电气架构 --- 智能座舱功能应用

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节…

springCloudAlibaba之分布式网关组件---gateway

gateway-网关 网关spring cloud gatewaygateway初体验gateway整合nacos简写方式 网关 在微服务架构中一个系统会被拆分成多个微服务。那么作为客户端(前端)要如何去调用这么多的微服务&#xff1f;如果没有网关的存在&#xff0c;我们只能在客户端记录每个微服务的地址&#xf…

工程设计问题---多盘离合器制动器设计问题

这个问题的主要目的是使多片式离合器制动器的质量最小化。在这个问题中&#xff0c;使用了五个整数决策变量&#xff0c;它们是内半径&#xff08;x1&#xff09;、外半径&#xff08;x2&#xff09;、盘厚度&#xff08;x3&#xff09;、致动器的力&#xff08;x4&#xff09;…

【Mybatis-Plus】根据自定义注解实现自动加解密

背景 我们把数据存到数据库的时候&#xff0c;有些敏感字段是需要加密的&#xff0c;从数据库查出来再进行解密。如果存在多张表或者多个地方需要对部分字段进行加解密操作&#xff0c;每个地方都手写一次加解密的动作&#xff0c;显然不是最好的选择。如果我们使用的是Mybati…

基于WPF技术的换热站智能监控系统13--控制设备开关

1、本节目的 本次工作量相对有点大&#xff0c;有点难度&#xff0c;需要熟悉MVVM模式&#xff0c;特别是属性绑定和命令驱动&#xff0c;目标是点击水泵开关&#xff0c;让风扇转动或停止&#xff0c;风扇连接的管道液体流动或静止。 &#xff0c;具体对应关系是&#xff1a;…

单向散列函数解析

目录 1. 概述 2. 单向散列函数的性质 2.1 根据任意长度的消息计算出固定长度的散列值 2.2 能够快速计算出散列值 2.3 消息不同散列值也不同 2.4 具备单向性 3. 单向散列函数的算法 3.1 MD5 3.2 SHA序列 3.3 SM3 1. 概述 针对计算机所处理的消息&#xff0c;有时候我们…

【设计模式深度剖析】【9】【行为型】【访问者模式】| 以博物馆的导览员为例加深理解

&#x1f448;️上一篇:备忘录模式 设计模式-专栏&#x1f448;️ 文章目录 访问者模式定义英文原话直译如何理解呢&#xff1f; 访问者模式的角色类图代码示例 访问者模式的应用优点缺点使用场景 示例解析:博物馆的导览员代码示例 访问者模式 访问者模式&#xff08;Visito…

计算机毕业设计hadoop+spark+hive知识图谱酒店推荐系统 酒店数据分析可视化大屏 酒店爬虫 高德地图API 酒店预测系统 大数据毕业设计

流程&#xff1a; 1.Python爬取去哪儿网全站旅游数据约10万&#xff0c;存入mysql; 2.使用pandasnumpy/hadoopmapreduce对mysql中旅游数据进行数据清洗&#xff0c;使用高德API计算地理信息&#xff0c;最终转为.csv文件上传hdfs; 3.hive建库建表导入.csv文件作为数据集&#x…

2024年汉字小达人活动还有4个多月开赛:来做18道历年选择题备考吧

结合最近几年的活动安排&#xff0c;预计2024年第11届汉字小达人比赛还有4个多月就启动&#xff0c;那么孩子们如何利用这段时间有条不紊地准备汉字小达人比赛呢&#xff1f; 我的建议是充分利用即将到来的暑假&#xff1a;①把小学1-5年级的语文课本上的知识点熟悉&#xff0…

Windows11和Ubuntu22双系统安装指南

一、需求描述 台式机电脑&#xff0c;已有Windows11操作系统&#xff0c;想要安装Ubuntu22系统&#xff08;版本任意&#xff09;。其中Windows安装在Nvme固态上&#xff0c;Ubuntu安装在Sata固态上&#xff0c;双盘双系统。开机时使用Grub控制进入哪个系统&#xff0c;效果图…

直接选择排序-C语言版本

前言 直接选择排序也是一个比较简单的排序&#xff0c;所以这里放在第二个进行讲解&#xff0c;这里和冒泡排序是有一点相似。直接选择排序和冒泡排序一样&#xff0c;也是具备一定的教学意义&#xff0c;但是没有什么实际操作的意义&#xff0c;因为直接选择排序的时间复杂度比…

云原生 Docker Swarm 使用详解

Hi~&#xff01;这里是奋斗的小羊&#xff0c;很荣幸您能阅读我的文章&#xff0c;诚请评论指点&#xff0c;欢迎欢迎 ~~ &#x1f4a5;&#x1f4a5;个人主页&#xff1a;奋斗的小羊 &#x1f4a5;&#x1f4a5;所属专栏&#xff1a;C语言 &#x1f680;本系列文章为个人学习…

用Copilot画漫画,Luma AI生成视频:解锁创意新玩法

近年来&#xff0c;随着人工智能技术的不断发展&#xff0c;各种创意工具也层出不穷。今天&#xff0c;我们就来介绍一种全新的创作方式&#xff1a;使用Copilot画漫画&#xff0c;再将漫画放入Luma AI生成视频。 Copilot&#xff1a;你的AI绘画助手 Copilot是一款基于人工智…

Java | Leetcode Java题解之第147题对链表进行插入排序

题目&#xff1a; 题解&#xff1a; class Solution {public ListNode insertionSortList(ListNode head) {if (head null) {return head;}ListNode dummyHead new ListNode(0);dummyHead.next head;ListNode lastSorted head, curr head.next;while (curr ! null) {if (…