【Perl】与【Excel】



引言

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.mfbz.cn/a/714250.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Unity的三种Update方法

1、FixedUpdate 物理作用——处理物理引擎相关的计算和刚体的移动 (1) 调用时机&#xff1a;在固定的时间间隔内&#xff0c;而不是每一帧被调用 (2) 作用&#xff1a;用于处理物理引擎的计算&#xff0c;例如刚体的移动和碰撞检测 (3) 特点&#xff1a;能更准确地处理物理…

【算法】某赛车游戏中的组合计数问题及其扩展。推导思路:层层合并

文章目录 引言所有人都能完成可能有人未完成扩展问题参考资料 引言 在某款人称赛车界原神的赛车游戏中有组队竞速赛。共有n个人&#xff0c;n为偶数&#xff0c;分为人数相等的红队和蓝队进行比赛。结果按排名得分的数组为pts&#xff0c;单调递减且均为正整数。比如pts [10,…

算法day28

第一题 295. 数据流的中位数 本题我们是求解给定数组的中位数。且由于需要随时给数组添加元素&#xff0c;所以我们要求解该动态数组的中位数&#xff0c;所以本题最关键的就是维护数组在添加元素之后保持有序的排序&#xff0c;这样就能很快的求解中位数&#xff1b; 解法&am…

C++11完美转发(引用折叠、万能引用)

完美转发是指在函数模板中&#xff0c;完全依照模板的参数的类型&#xff0c;将参数传递给函数模板中调用的另外一个函数。 函数模板在向其他函数传递自身形参时&#xff0c;如果相应实参是左值&#xff0c;它就应该被转发为左值&#xff1b;如果相 应实参是右值&#xff0c;它…

web安全渗透测试十大常规项(一):web渗透测试之PHP反序列化

渗透测试之XSS跨站脚本攻击 1. PHP反序列化1.1 什么是反序列化操作? - 类型转换1.2 常见PHP魔术方法?- 对象逻辑(见图)1.2.1 construct和destruct1.2.2 construct和sleep1.2.2 construct和wakeup1.2.2 INVOKE1.2.2 toString1.2.2 CALL1.2.2 get()1.2.2 set()1.2.2 isset()1…

查看npm版本异常,更新nvm版本解决问题

首先说说遇见的问题&#xff0c;基本上把nvm&#xff0c;npm的坑都排了一遍 nvm版本导致npm install报错 Unexpected token ‘.‘install和查看node版本都正确&#xff0c;结果查看npm版本时候报错 首先就是降低node版本… 可以说基本没用&#xff0c;如果要降低版本的话&…

linxu-Ubuntu系统上卸载Kubernetes-k8s

如果您想从Ubuntu系统上卸载Kubernetes集群&#xff0c;您需要执行以下步骤&#xff1a; 1.关闭Kubernetes集群&#xff1a; 如果您的集群还在运行&#xff0c;首先您需要使用kubeadm命令来安全地关闭它&#xff1a; sudo kubeadm reset在执行该命令后&#xff0c;系统会提示…

【JavaEE进阶】——利用框架完成功能全面的图书管理系统

目录 &#x1f6a9;项目所需要的技术栈 &#x1f6a9;项目准备工作 &#x1f388;环境准备 &#x1f388;数据库准备 &#x1f6a9;前后端交互分析 &#x1f388;登录 &#x1f4dd;前后端交互 &#x1f4dd;实现服务器代码 &#x1f4dd;测试前后端代码是否正确 &am…

01 - matlab m_map地学绘图工具基础函数理解(一)

01 - matlab m_map地学绘图工具基础函数理解&#xff08;一&#xff09; 0. 引言1. m_demo2. 小结 0. 引言 上篇介绍了m_map的配置过程&#xff0c;本篇开始介绍下m_map中涉及到的所有可调用函数。如果配置的没有问题&#xff0c;执行">>help m_map"可以看到类…

【C++】C++入门的杂碎知识点

思维导图大纲&#xff1a; namespac命名空间 什么是namespace命名空间namespace命名空间有什么用 什么是命名空间 namespace命名空间是一种域&#xff0c;它可以将内部的成员隔绝起来。举个例子&#xff0c;我们都知道有全局变量和局部变量&#xff0c;全局变量存在于全局域…

趣味C语言——【猜数字】小游戏

&#x1f970;欢迎关注 轻松拿捏C语言系列&#xff0c;来和 小哇 一起进步&#xff01;✊ &#x1f389;创作不易&#xff0c;请多多支持&#x1f389; &#x1f308;感谢大家的阅读、点赞、收藏和关注&#x1f495; &#x1f339;如有问题&#xff0c;欢迎指正 感谢 目录 代码…

抖音混剪素材哪里找?可以混剪搬运视频素材网站分享

在抖音上制作精彩的视频离不开高质量的素材资源。今天&#xff0c;我将为大家推荐几个优质的网站&#xff0c;帮助你解决素材短缺的问题。这些网站不仅提供丰富的素材&#xff0c;还符合百度SEO优化的规则&#xff0c;让你的视频更容易被发现。 蛙学府素材网 首先要推荐的是蛙…

模拟自动滚动并展开所有评论列表以及回复内容(如:抖音、b站等平台)

由于各大视频平台的回复内容排序不都是按照时间顺序&#xff0c;而且想看最新的评论回复讨论内容还需逐个点击展开&#xff0c;真的很蛋疼&#xff0c;尤其是热评很多的情况&#xff0c;还需要多次点击展开&#xff0c;太麻烦&#xff01; 于是写了一个自动化展开所有评论回复…

诊断解决方案——CANdesc和MICROSAR

文章目录 一、CANdesc二、MICROSAR一、CANdesc canbeded是Vector汽车电子开发软件Nun Autosar标准的工具链之一。 canbeded是以源代码的形式提供的可重用的组件,包括CAN Driver,交互层(IL),网络管理(NM),传输层(TP),诊断层(CANdesc) , 通信测量和标定协议(CCP,XCP) 和 通信控…

Es 索引查询排序分析

文章目录 概要一、Es数据存储1.1、_source1.2、stored fields 二、Doc values2.1、FieldCache2.2、DocValues 三、Fielddata四、Index sorting五、小结六、参考 概要 倒排索引 优势在于快速的查找到包含特定关键词的所有文档&#xff0c;但是排序&#xff0c;过滤、聚合等操作…

并发容器(二):Concurrent类下的ConcurrentHashMap源码级解析

并发容器-ConcurrentHashMap 前言数据结构JDK1.7版本HashEntrySegment 初始化 重要方法Put方法扩容rehash方法 前言 之前我们在集合篇里聊完了HashMap和HashTable&#xff0c;我们又学习了并发编程的基本内容&#xff0c;现在我们来聊一聊Concurrent类下的重要容器&#xff0c…

tsp可视化python

随机生成点的坐标并依据点集生成距离矩阵&#xff0c;通过点的坐标实现可视化 c代码看我的这篇文章tsp动态规划递归解法c from typing import List, Tuple import matplotlib.pyplot as plt from random import randintN: int 4 MAX: int 0x7f7f7f7fdistances: List[List[in…

最长不下降子序列LIS详解

最长不下降子序列指的是在一个数字序列中&#xff0c;找到一个最长的子序列&#xff08;可以不连续&#xff09;&#xff0c;使得这个子序列是不下降&#xff08;非递减&#xff09;的。 假如&#xff0c;现有序列A[1&#xff0c;2&#xff0c;3&#xff0c;-1&#xff0c;-2&…

60.WEB渗透测试-信息收集- 端口、目录扫描、源码泄露(8)

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 内容参考于&#xff1a; 易锦网校会员专享课 上一个内容&#xff1a;59.WEB渗透测试-信息收集- 端口、目录扫描、源码泄露&#xff08;7&#xff09; 御剑是用…

植物大战僵尸杂交版全新版v2.1解决全屏问题

文章目录 &#x1f68b;一、植物大战僵尸杂交版❤️1. 游戏介绍&#x1f4a5;2. 如何下载《植物大战僵尸杂交版》 &#x1f680;二、解决最新2.1版的全屏问题&#x1f308;三、画质增强以及减少闪退 &#x1f68b;一、植物大战僵尸杂交版 《植物大战僵尸杂交版》是一款在原版《…